package com.ruoyi.project.lely.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.framework.aspectj.lang.annotation.DataSource;
import com.ruoyi.framework.aspectj.lang.enums.DataSourceType;
import com.ruoyi.project.lely.bean.dto.PageStudentDTO;
import com.ruoyi.project.lely.bean.entity.Student;
import com.ruoyi.project.lely.bean.vo.StudentVo;
import com.ruoyi.common.R;
import com.ruoyi.project.lely.mapper.SysStudentMapper;
import com.ruoyi.project.lely.service.ISysStudentService;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 学生信息Service业务层处理
 *
 * @author luoyong
 */
@Service
@DataSource(value = DataSourceType.SLAVE)
public class SysStudentServiceImpl extends ServiceImpl<SysStudentMapper, Student> implements ISysStudentService {

    @Autowired
    private SysStudentMapper studentMapper;

    /**
     * 学生列表-全部
     *
     * @return
     */
    @Override
    public R getAllList() {
        List<Student> list = this.list(new LambdaQueryWrapper<Student>().orderByDesc(Student::getCreateTime));
        if (CollUtil.isNotEmpty(list)) {
            List<StudentVo> vos = new ArrayList<>();
            list.stream().forEach(n -> {
                StudentVo vo = new StudentVo();
                BeanUtils.copyProperties(n, vo);
                vos.add(vo);
            });
            return R.ok(vos);
        }
        return null;
    }

    /**
     * 学生列表-分页
     *
     * @param dto 查询条件
     * @return
     */
    @Override
    public R getPage(PageStudentDTO dto) {

        Page<Student> StudentPage = new Page<>(dto.getPageCurrent(), dto.getPageSize(), false);
        IPage page = studentMapper.selectPage(
                StudentPage,
                new LambdaQueryWrapper<Student>()
                        .eq(ObjectUtil.isNotEmpty(dto.getName()), Student::getName, dto.getName())
                        .eq(ObjectUtil.isNotEmpty(dto.getAge()), Student::getAge, dto.getAge())
                        .eq(ObjectUtil.isNotEmpty(dto.getHobby()), Student::getHobby, dto.getHobby())
                        .orderByDesc(Student::getCreateTime)
        );
        List<Student> list = page.getRecords();
        if (CollUtil.isNotEmpty(list)) {
            List<StudentVo> vos = new ArrayList<>();
            list.stream().forEach(n -> {
                StudentVo vo = new StudentVo();
                BeanUtils.copyProperties(n, vo);
                vos.add(vo);
            });
            page.setRecords(vos);
            return R.ok(page);
        }

        return R.ok(list);
    }

    /**
     * 新增学生
     *
     * @param student
     * @return
     */
    @Override
    public R addOrUpdate(Student student) {
        List<Student> list = this.list(
                new LambdaQueryWrapper<Student>()
                        .eq(Student::getName, student.getName())
                        .ne(ObjectUtil.isNotNull(student.getId()), Student::getId, student.getId())
        );
        if (CollUtil.isNotEmpty(list)) {
            return R.fail("当前用户名已存在");
        }
        boolean save = this.saveOrUpdate(student);
        return save ? R.ok("操作成功") : R.fail("操作失败");
    }
}
